import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 与Kotlin代码通信
class ServiceApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Sample Shared App Handler',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: ServiceAppPage(),
    );
  }
}

/// 页面
class ServiceAppPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => ServiceAppPageState();
}

/// 页面状态
class ServiceAppPageState extends State<ServiceAppPage> {
  static const platform = const MethodChannel('app.channel.shared.data');
  String dataShared = 'No data';

  @override
  void initState() {
    super.initState();
    getSharedText();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(dataShared),
      ),
    );
  }

  getSharedText() async {
    var sharedData = await platform.invokeMethod("getSharedText");
    if (sharedData != null) {
      setState(() {
        dataShared = sharedData;
      });
    }
  }
}
